//
//  MRMMobileRechargeViewController.m
//  MeiRiMoney
//
//  Created by qiuqk on 13-6-29.
//  Copyright (c) 2013年 MeiRiCai. All rights reserved.
//

#import "MRMMobileRechargeViewController.h"
#import "MRMUtils.h"
#import "MRMSdk.h"

@interface MRMMobileRechargeViewController ()

@end

@implementation MRMMobileRechargeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
	
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	
	self.mobileTextField.delegate = self;
	self.mobile2TextField.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setMobileTextField:nil];
    [self setMobile2TextField:nil];
    [self setOkButton:nil];
    [super viewDidUnload];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	return TRUE;
}

- (IBAction)okButtonTouchUpInside:(id)sender
{
	static NSString *mobilePrefix = @"134|135|136|137|138|139|147|150|151|152|157|158|159|182|183|184|187|188|130|131|132|145|155|156|185|186|133|153|180|181|189";
	NSString *mobilePattern = [NSString stringWithFormat:@"^(%@)[\\d]{8}$", mobilePrefix];
	
	NSString *mobile = [self.mobileTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
	NSString *mobile2 = [self.mobile2TextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
	
	// 输入的数据是否为手机号码格式
	NSError *error;
	NSRegularExpression *mobileRegular = [[NSRegularExpression alloc] initWithPattern:mobilePattern options:NSRegularExpressionCaseInsensitive error:&error];
	NSUInteger number = [mobileRegular numberOfMatchesInString:mobile options:0 range:NSMakeRange(0, [mobile length])];
	if (number != 1)
	{
		UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误提示" message:@"手机号码格式不对" delegate:nil cancelButtonTitle:@"重填" otherButtonTitles:nil];
		[alertView show];
		return;
	}
	// 两次输入的手机号码是否一直
	if(![mobile isEqual:mobile2])
	{
		UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误提示" message:@"两次输入的手机号码不一致" delegate:nil cancelButtonTitle:@"重填" otherButtonTitles:nil];
		[alertView show];
		return;
	}
	
	UIAlertView *confirmView = [[UIAlertView alloc] initWithTitle:@"请确认手机号码" message:mobile delegate:self cancelButtonTitle:@"重填" otherButtonTitles:@"确认", nil];
	[confirmView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if (buttonIndex == 1) {
		NSString *shipAddress = [self.mobileTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
		NSLog(@"%@", shipAddress);
		
		[MRMSdk exchangeCommodity:self.commodity.Id number:1 shipAddress:shipAddress];
		
		[MRMUtils showExchangeCompleteView:self];
	}
}



@end
